﻿using System;
using NExt.Handler.Base.Common.Manager;

namespace NExt.Handler.Component.Default.JsSerialize
{
    public class TypeRegistrationScriptGenerator : AbstractNExtScriptGenerator
    {
        private const string SCRIPT_POSTFIX = "TypeRegistration.js";
        private const string REGISTRATION_SCRIPT_FORMAT = "NExt.regType('{0}', {1});";

        public TypeRegistrationScriptGenerator()
            : base(SCRIPT_POSTFIX)
        {
        }

        protected override string BuildDefaultContent(Type type)
        {
            if (!typeof(NExt.Core.Model.Base.Component).IsAssignableFrom(type))
                return string.Empty;

            object instance = InstanceManager.GetInstance(type);
            NExt.Core.Model.Base.Component nextObject = (NExt.Core.Model.Base.Component)instance;
            string xtype = nextObject.xtype;
            string fullName = type.FullName;

            string content = string.Format(REGISTRATION_SCRIPT_FORMAT, xtype, fullName);
            return content;
        }
    }
}
